﻿using System;
using System.Configuration;
using System.Linq;
using OS.Infrastructure.Common;
using TaskKernel.BootStrapper;
using TaskKernel.Configuration;
using TaskKernel.Domain;
using TaskKernel.Domain.Services.Mail;
using TaskKernel.Infrastructure;
using Xunit;

namespace TaskKernel.Tests
{
    public class MessageServiceTest
    {
    
        public MessageServiceTest()
        {
            TaskKernelBootStrapper.RunConsoleApp();
        }

        //[Fact]
        //public void testChangeNotification()
        //{
        //    //arrange
        //    var messageProcessor = IoC.Resolve<IMessageService>();
        //    var dateTime = DateTime.Now;
        //    var account = new Account("olegk").SetFullName("Mihail Zakutkov");
        //    account.SetMail("olegk@servertd.spb.ru");
        //    var account2 = new Account("luck").SetFullName("luck");
        //    account2.SetMail("oleg@servertd.spb.ru");
        //    var task = new DomainModelTest().create_task(account, "subject");
        //    var department = new Department("/dep") { DepartmentName = "Department Name" };
        //    task.SetDepartment(account, department);
        //    task.AddHistoryEntry(account, "First Comment");
        //     var file = new File("filename.txt", new byte[0], 0);
        //    task.AddFile(account, file);
        //    task.AddReminder(account2, dateTime, "Reminder Text Here");
        //    //act
        //    messageProcessor.ChangeNotification(task, account2, "Задача изменена");
        //    var messageModel = messageProcessor.Message;
        //    //arrange
        //    var bodyNewTask = @"Действие: Задача изменена<br>Инициатор: Mihail Zakutkov<br>Владелец: Mihail Zakutkov<br>Участники: <br>Ссылка: <a href=""http://portal2007/dep/_layouts/webtasks/EditTask.aspx?ID=0"">subject</a><br>Ссылка в Интернете: <a href=""https://portal2007.servertd.spb.ru:27277/dep/_layouts/webtasks/EditTask.aspx?ID=0"">subject</a><table><tr><td>"+dateTime+@"</td><td><font color=""red"">Задача передана на исполнение в раздел: Department Name</font></td></tr><tr><td>" + dateTime + "</td><td>First Comment</td></tr><tr><td>" + dateTime + "</td><td>filename.txt</td></tr></table>";
        //    Assert.Equal(bodyNewTask, messageModel.MessageBody);
        //    Assert.Equal("subject", messageModel.MessageSubject);
        //    Assert.Equal("Department Name", messageModel.MessageSenderDisplayName);
        //    Assert.Equal("webtasks@servertd.spb.ru", messageModel.MessageSender);
        //    Assert.True(messageProcessor.Sended);
        //}

        //[Fact]
        //public void TestReminderNotification()
        //{
        //    //arrange
        //    var messageProcessor = IoC.Resolve<IMessageService>();
        //    var dateTime = DateTime.Now;
        //    var account = new Account("olegk").SetFullName("Mihail Zakutkov");
        //    account.SetMail("olegk@servertd.spb.ru");
        //    var task = new DomainModelTest().create_task(account, "subject");
        //    var department = new Department("/dep") { DepartmentName = "Department Name" };
        //    task.SetDepartment(account, department);
        //    task.AddHistoryEntry(account, "First Comment");
        //    var file = new File("filename.txt", new byte[0], 0);
        //    task.AddFile(account, file);
        //    task.AddReminder(account, dateTime, "Reminder Text Here");
        //    //act
        //    messageProcessor.ReminderNotification(task.Reminders.Where(x=>x.DateTime==dateTime).First());
        //    var messageModel = messageProcessor.Message;
        //    //arrange
        //    var bodyNewTask = @"Действие: Напоминание<br>Текст напоминания: Reminder Text Here<br>Владелец: Mihail Zakutkov<br>Участники: <br>Ссылка: <a href=""http://portal2007/dep/_layouts/webtasks/EditTask.aspx?ID=0"">subject</a><br>Ссылка в Интернете: <a href=""https://portal2007.servertd.spb.ru:27277/dep/_layouts/webtasks/EditTask.aspx?ID=0"">subject</a>";
        //    Assert.Equal(bodyNewTask, messageModel.MessageBody);
        //    Assert.Equal("subject [Напоминание]", messageModel.MessageSubject);
        //    Assert.Equal("Напоминание", messageModel.MessageSenderDisplayName);
        //    Assert.Equal("webtasks@servertd.spb.ru", messageModel.MessageSender);
        //    Assert.True(messageProcessor.Sended);
        //}

        //[Fact]
        //public void TestRememberCloseNotification()
        //{
        //    //arrange
        //    var messageProcessor = IoC.Resolve<IMessageService>();
        //    var account = new Account("olegk").SetFullName("Mihail Zakutkov");
        //    account.SetMail("olegk@servertd.spb.ru");
        //    var task = new DomainModelTest().create_task(account, "subject");
        //    var department = new Department("/dep") { DepartmentName = "Department Name" };
        //    task.SetDepartment(account, department);
        //    task.AddHistoryEntry(account, "First Comment");
        //    var file = new File("filename.txt", new byte[0], 0);
        //    task.AddFile(account, file);
        //    //act
        //    messageProcessor.RememberCloseNotification(task);
        //    var messageModel = messageProcessor.Message;
        //    //arrange
        //    var bodyNewTask = @"Действие: Необходимо закрыть задачу<br>Владелец: Mihail Zakutkov<br>Участники: <br>Ссылка: <a href=""http://portal2007/dep/_layouts/webtasks/EditTask.aspx?ID=0"">subject</a><br>Ссылка в Интернете: <a href=""https://portal2007.servertd.spb.ru:27277/dep/_layouts/webtasks/EditTask.aspx?ID=0"">subject</a>";
        //    Assert.Equal(bodyNewTask, messageModel.MessageBody);
        //    Assert.Equal("subject [Необходимо закрыть задачу]", messageModel.MessageSubject);
        //    Assert.Equal("Напоминание", messageModel.MessageSenderDisplayName);
        //    Assert.Equal("webtasks@servertd.spb.ru", messageModel.MessageSender);
        //    Assert.True(messageProcessor.Sended);
        //}

        //[Fact]
        //public void TestContentAlexandrovaNotification()
        //{
        //    //arrange
        //    var messageProcessor = IoC.Resolve<IMessageService>();
        //    var account = new Account("olegk").SetFullName("Mihail Zakutkov");
        //    account.SetMail("olegk@servertd.spb.ru");
        //    var task = new DomainModelTest().create_task(account, "subject");
        //    var department = new Department("/dep") { DepartmentName = "Department Name" };
        //    task.SetDepartment(account, department);
        //    var time = DateTime.Now;
        //    //act
        //    task.AddHistoryEntry(account, "First Comment");
        //   // task.AddFile(account, "filename.txt", Tools.ReadByteArrayFromFile("../../test.txt"));
        //    messageProcessor.ContentAlexandrovaNotification(task, account);
        //    var messageModel = messageProcessor.Message;
        //    //arrange
        //    var bodyNewTask = @"<table><tr><td>" +
        //                      time + "</td><td>First Comment</td><td>Mihail Zakutkov</td></tr><tr><td>" + time +
        //                      "</td><td>filename.txt</td><td>Mihail Zakutkov</td></tr></table>";
        //    Assert.Equal(bodyNewTask, messageModel.MessageBody);
        //    Assert.Equal("subject", messageModel.MessageSubject);
        //    Assert.Equal("Department Name", messageModel.MessageSenderDisplayName);
        //    Assert.Equal("olegk@servertd.spb.ru", messageModel.MessageSender);
        //    Assert.True(messageProcessor.Sended);
        //}

        [Fact]
        public void CheckBodyContentAlexandrova()
        {
            //arrange
            var account = new Account("olegk").SetFullName("Mihail Zakutkov");
            account.SetMail("olegk@servertd.spb.ru");
            var task = new DomainModelTest().create_task(account, "subject");
            var time = DateTime.Now;
            //act
            task.AddHistoryEntry(account, "First Comment");
            var file = new File("filename.txt", new byte[0], 0);
            task.AddFile(account, file);
            var service = new BodyTemplateContentAlexandrova(task);
            var body = service.GetBody();
            //assert
            Assert.Equal(@"<table><tr><td valign=""top"">" + time +
                         @"</td><td>First Comment</td><td>Mihail Zakutkov</td></tr><tr><td valign=""top"">" + time +
                         "</td><td>filename.txt</td><td>Mihail Zakutkov</td></tr></table>", body);
        }

      
//        [Fact]
//        public void SendNotifyNewTask()
//        {
//            var messageProcessor = _ioc.Resolve<IMessageService>();
//            var account = new Account("olegk", 0).SetFullName("blablaowner");
//            account.SetMail("olegk@servertd.spb.ru");
//            var account2 = new Account("luck", 1).SetFullName("luck");
//            account2.SetMail("olegk@servertd.spb.ru");
//            var task = new DomainModelTest().create_task(account, "subject");
//            task.AddHistoryEntry(account, "efef");
//            task.AddFile(account, "test.txt", Tools.ReadByteArrayFromFile("../../test.txt"));
//            messageProcessor.NewNotification(task);
//            var messageModel = messageProcessor.Message;
//            var bodyNewTask =
//                @"Действие: Новая задача
//Инициатор: Прокопьева Анна Вячеславовна
//Владелец: Прокопьева Анна Вячеславовна
//Ссылка: нет доступа к печати с Питерской базы
//Ссылка в Интернете: нет доступа к печати с Питерской базы
//Комментарий: 
//22.05.2012 11:17:40: IP: 192.168.30.77
//22.05.2012 11:17:40: добрый день!
//сделайте доступ к печати с Питерской базы срочно! спс!
//";
//            Assert.Equal(bodyNewTask, messageModel.MessageBody);
//            Assert.Equal("subject", messageModel.MessageSubject);
//            Assert.True(messageProcessor.Sended);
//        }

        [Fact]
        public void SendRequestCloseNotify()
        {
            var account = new Account("olegk").SetFullName("blablaowner");
            account.SetMail("olegk@servertd.spb.ru");
            var account2 = new Account("luck").SetFullName("luck");
            account2.SetMail("olegk@servertd.spb.ru");
            //   var tracker = new TrackerServiceRequestClose();
            var task = new DomainModelTest().create_task(account, "taskName");
            task.AddHistoryEntry(account, "efef");
            task.AddHistoryEntry(account, "wefwfwefweff");
            //task.AddFile(account, "test.txt", Tools.ReadByteArrayFromFile("../../test.txt"));
            //task.AddFile(account, "test2.txt", Tools.ReadByteArrayFromFile("../../test.txt"));
            task.RequestClose(account);
            var sender = new SmtpSender(ConfigurationManager.AppSettings["smtpServer"]);
            //      tracker.RequestClose(task);
            //var messageService = new MessageServiceRequestClose(sender, task);
            //var sended = messageService.SendMessage();
            //Assert.True(sended);
        }
    }
}